package com.lagou.hbase.relation;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;

public class RelationDemo {
    Configuration conf = null;
    Connection conn = null;
    @Before
    public void init() throws IOException {
        //获取一个配置文件
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取hbase集群的连接
        conn = ConnectionFactory.createConnection(conf);

    }
    //创建表
    @Test
    public void createTbale() throws IOException {
        //获取hbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        final HTableDescriptor relation = new HTableDescriptor(TableName.valueOf("relation"));
        //指定列组
        relation.addFamily(new HColumnDescriptor("friends"));
        admin.createTable(relation);
        System.out.println("relation表创建成功");
    }
    //插入数据
    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table relation = conn.getTable(TableName.valueOf("relation"));
        //准备put对象
        final Put put1 = new Put(Bytes.toBytes("uid1"));//指定rowkey
        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        put1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));
        final Put put2 = new Put(Bytes.toBytes("uid2"));//指定rowkey
        put2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        put2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        put2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));
        //准备list<puts>,可以执行批量插入
        final ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(put1);
        puts.add(put2);
        relation.put(puts);
        //关闭table对象
        relation.close();
        System.out.println("插入数据到relation表成功");

    }
    //删除数据
    @Test
    public void deleteData() throws IOException {
        //需要获取一个table对象
        final Table relation = conn.getTable(TableName.valueOf("relation"));
        //准备delete对象
        final Delete delete = new Delete(Bytes.toBytes("uid2"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"));
        //执行删除
        relation.delete(delete);
        //关闭table对象
        relation.close();
        System.out.println("删除数据成功");
    }
    //释放连接
    @After
    public void relase() {
        if(conn!=null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
